package com.catfive.jet_work.feature.dashboard

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.catfive.jet_work.R
import com.catfive.jet_work.core.design.component.TopBarIconMenuButton
import com.hitanshudhawan.circularprogressbar.CircularProgressBar

@Composable
fun DashboardRoute(onOpenDrawer: () -> Unit) {
    DashboardScreen(onOpenDrawer = onOpenDrawer)
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DashboardScreen(onOpenDrawer: () -> Unit = fun() {}) {
    Scaffold(topBar = {
        CenterAlignedTopAppBar(
            colors = TopAppBarDefaults.topAppBarColors(
                containerColor = MaterialTheme.colorScheme.surfaceVariant.copy(0.6f)
            ),
            navigationIcon = {
                TopBarIconMenuButton(onClick = { onOpenDrawer() })
//                Icon(
//                    modifier = Modifier
//                        .padding(start = 16.dp, end = 8.dp)
//                        .size(24.dp)
//                        .clickable { onOpenDrawer() },
//                    imageVector = Icons.Default.Menu,
//                    contentDescription = ""
//                )
            },
            title = {
                Text("仪表盘", fontSize = 18.sp)
            }
        )
    }
    ) { paddingValues ->
        val progress by animateFloatAsState(10f)
        Surface(modifier = Modifier.padding(paddingValues)) {
            Column(modifier = Modifier.padding(8.dp)) {
                LazyVerticalGrid(
                    columns = GridCells.Fixed(2),
                    modifier = Modifier.fillMaxWidth(),
                    contentPadding = PaddingValues(8.dp),
                    verticalArrangement = Arrangement.spacedBy(8.dp),
                    horizontalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    item { ServerLoadCard() }
                    item { CPULoadCard() }
                    item { MemCard() }
                    item { DiskCard() }
                }
            }
        }
    }
}

// 负载
@Composable
fun ServerLoadCard() {
    Card(
        colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.background),
        shape = RoundedCornerShape(15.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 5.dp),
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(vertical = 10.dp),
        )
        {
            CircularProgressBar(
                modifier = Modifier
                    .size(160.dp)
                    .align(Alignment.Center),
                progress = 30f,
                progressMax = 100f,
                progressBarWidth = 5.dp,
                backgroundProgressBarWidth = 10.dp,
                progressBarColor = Color.Red,
                backgroundProgressBarColor = colorResource(R.color.process_bar),
                roundBorder = true,
            )
            Column(
                modifier = Modifier
                    .align(Alignment.Center)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = "负载",
                    color = colorResource(R.color.process_bar),
                    fontSize = 18.sp
                )
                Text(text = "0%", color = Color.Magenta, fontSize = 22.sp)
                Text(
                    text = "运行流畅",
                    color = colorResource(R.color.process_bar),
                    fontSize = 18.sp,
                    modifier = Modifier.padding(top = 4.dp)
                )
            }
        }
    }
}

@Composable
fun CPULoadCard() {
    Card(
        colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.background),
        shape = RoundedCornerShape(15.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 5.dp),
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(vertical = 10.dp),
        )
        {
            CircularProgressBar(
                modifier = Modifier
                    .size(160.dp)
                    .align(Alignment.Center),
                progress = 30f,
                progressMax = 100f,
                progressBarWidth = 5.dp,
                backgroundProgressBarWidth = 10.dp,
                progressBarColor = Color.Red,
                backgroundProgressBarColor = colorResource(R.color.process_bar),
                roundBorder = true,
            )
            Column(
                modifier = Modifier
                    .align(Alignment.Center)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = "CPU",
                    color = colorResource(R.color.process_bar),
                    fontSize = 18.sp
                )
                Text(text = "2%", color = Color.Magenta, fontSize = 22.sp)
                Text(
                    text = "2核心",
                    color = colorResource(R.color.process_bar),
                    fontSize = 18.sp,
                    modifier = Modifier.padding(top = 4.dp)
                )
            }
        }
    }
}


@Composable
fun MemCard() {
    Card(
        colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.background),
        shape = RoundedCornerShape(15.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 5.dp),
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(vertical = 10.dp),
        )
        {
            CircularProgressBar(
                modifier = Modifier
                    .size(160.dp)
                    .align(Alignment.Center),
                progress = 30f,
                progressMax = 100f,
                progressBarWidth = 5.dp,
                backgroundProgressBarWidth = 10.dp,
                progressBarColor = Color.Red,
                backgroundProgressBarColor = colorResource(R.color.process_bar),
                roundBorder = true,
            )
            Column(
                modifier = Modifier
                    .align(Alignment.Center)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = "内存",
                    color = colorResource(R.color.process_bar),
                    fontSize = 18.sp
                )
                Text(text = "23.5%", color = Color.Magenta, fontSize = 22.sp)
                Text(
                    text = "838.0MB/3GB",
                    color = colorResource(R.color.process_bar),
                    fontSize = 18.sp,
                    modifier = Modifier.padding(top = 4.dp)
                )
            }
        }
    }
}

@Composable
fun DiskCard() {
    Card(
        colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.background),
        shape = RoundedCornerShape(15.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 5.dp),
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(vertical = 10.dp),
        )
        {
            CircularProgressBar(
                modifier = Modifier
                    .size(160.dp)
                    .align(Alignment.Center),
                progress = 30f,
                progressMax = 100f,
                progressBarWidth = 5.dp,
                backgroundProgressBarWidth = 10.dp,
                progressBarColor = Color.Red,
                backgroundProgressBarColor = colorResource(R.color.process_bar),
                roundBorder = true,
            )
            Column(
                modifier = Modifier
                    .align(Alignment.Center)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = "挂载点： /",
                    color = colorResource(R.color.process_bar),
                    fontSize = 18.sp
                )
                Text(text = "13.29%", color = Color.Magenta, fontSize = 22.sp)
                Text(
                    text = "12/98GB",
                    color = colorResource(R.color.process_bar),
                    fontSize = 18.sp,
                    modifier = Modifier.padding(top = 4.dp)
                )
            }
        }
    }
}